﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InnovTech.Util.Persistence;
using NHibernate.Criterion;
using NHibernate.Linq;

namespace NHibernate.Util
{
    public class NHibernateDao : IDao
    {
        ISession _session;
        public NHibernateDao(ISession sess)
        {
            _session = sess;
        }

        public void Save(params object[] toSaveList)
        {
            if (toSaveList == null) return;
            foreach(object obj in toSaveList)
                _session.SaveOrUpdate(obj);
            _session.Flush();
        }

        public void Delete(params object[] toDeleteList)
        {
            if (toDeleteList == null) return;
            foreach (object obj in toDeleteList)
                _session.Delete(obj);
            _session.Flush();
        }

        public T Get<T>(object id)
        {
            return _session.Get<T>(id);
        }

        public IEnumerable<T> List<T>(Func<T, bool> predicate)
        {
            return _session.Query<T>().ToFuture().Where(t => predicate(t));
            //return from item in _session.Query<T>() where predicate(item) select item;
        }
    }
}
